from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QScrollArea, QPushButton

class Lyrics(QWidget):
    """管理歌词的类"""
    def __init__(self,MP3Player):
        super().__init__()
        self.lyricsBtn = QPushButton(self)
        self.lyrics_window = MP3Player.lyrics_window
        # 初始化
        self.lyricsBtn.setStyleSheet("QPushButton{border-image: url(:/resource/images/lyricsBtn.png)}")
        self.lyricsBtn.setFixedSize(32, 32)
        #SingalSlot
        self.lyricsBtn.clicked.connect(self.show_lyrics)

    def show_lyrics(self):
        """显示歌词的子窗口"""
        self.lyrics_window.show()

    def get_lyrics(self,path):
        """从文本文件中读取歌词"""
        try:
            with open(path, 'r', encoding='utf-8') as f:
                content = str(f.read())
        # except FileNotFoundError:
        #     print("无法打开指定的文件")
        # except LookupError:
        #     print("指定编码错误")
        # except UnicodeDecodeError:
        #     print("读取文件时解码错误")
        except FileNotFoundError:
            content = "你还没添加歌词呢"
        return content


class LyricsWindow(QWidget):
    """歌词弹窗类"""
    def __init__(self):
        super().__init__()
        #UI设计
        self.setWindowTitle("Lyrics")
        self.setWindowIcon(QIcon(':/resource/images/lyrics.png'))
        self.resize(300, 400)
        self.ly = QWidget()
        self.lyrics = QLabel(self.ly)
        #布局
        self.lyricsLayout = QVBoxLayout()
        self.lyricsLayout.addWidget(self.lyrics)
        self.ly.setLayout(self.lyricsLayout)

        self.scroll = QScrollArea()
        self.scroll.setWidgetResizable(True)    #滚动区域将自动调整窗口小部件的大小
        self.scroll.setWidget(self.ly)
        self.vbox = QVBoxLayout()
        self.vbox.addWidget(self.scroll)
        self.setLayout(self.vbox)

    def set_text(self,content):
        self.lyrics.setText(content)